home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / prof / profProfil.c < prev   
C/C++ Source or Header  |  1990-10-19  |  7KB  |  314 lines

  1. /* 
  2.  * profProfil.c --
  3.  *
  4.  *    Profil system call.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/prof/RCS/profProfil.c,v 9.3 90/10/19 15:52:28 rab Exp $";
  18. #endif /* not lint */
  19.  
  20. #include <assert.h>
  21. #include "sprite.h"
  22. #include "proc.h"
  23. #include "prof.h"
  24. #include "sync.h"
  25. #include "vm.h"
  26.  
  27. /*
  28.  * Program counter recorded by the interrupt handler.
  29.  */
  30. /* int Prof_InterruptPC; */
  31.  
  32. /*
  33.  * Monitor lock for this module.
  34.  */
  35. #ifndef lint
  36. static Sync_Lock profilLock = Sync_LockInitStatic("Prof:profilLock");
  37. #else
  38. static Sync_Lock profilLock;
  39. #endif
  40. #define LOCKPTR &profilLock
  41.  
  42. static void tick _ARGS_((Timer_Ticks time, ClientData clientData));
  43.  
  44. /*
  45.  * Information that is passed to Timer_ScheduleRoutine() to
  46.  * queue tick() for invocation by the call back timer
  47.  * interrupt handler.
  48.  */
  49. Timer_QueueElement profTimer_QueueElement = {
  50.     { NULL, NULL }, /* links */
  51.     tick,           /* routine */
  52.     0,              /* time */
  53.     1234,           /* clientData */
  54.     FALSE,          /* processed */
  55.     0               /* interval */
  56. };
  57.  
  58. /*
  59.  * Count of the number of processes being profiled.
  60.  */
  61. static int profCount = 0;
  62.  
  63.  
  64. /*
  65.  *----------------------------------------------------------------------
  66.  *
  67.  * profProfil --
  68.  *
  69.  *    Profil system call.
  70.  *
  71.  * Results:
  72.  *      Always returns success.
  73.  *
  74.  * Side effects:
  75.  *    Same as Prof_Enable.
  76.  *
  77.  *----------------------------------------------------------------------
  78.  */
  79.  
  80. ReturnStatus
  81. Prof_Profil(buffer, bufSize, offset, scale)
  82.     short *buffer;
  83.     int bufSize;
  84.     int offset;
  85.     int scale;
  86. {
  87.     Prof_Enable(Proc_GetCurrentProc(), buffer, bufSize, offset, scale);
  88.     return SUCCESS;
  89. }
  90.  
  91.  
  92. /*
  93.  *----------------------------------------------------------------------
  94.  *
  95.  * Prof_Enable --
  96.  *
  97.  *      Enables or disables the profiling of a process.
  98.  *
  99.  * Results:
  100.  *    None.
  101.  *
  102.  * Side effects:
  103.  *      Changes the Profiling fields in the Proc_ControlBlock, increments
  104.  *      or decrements ProfCount, and schedules or deschedules tick()
  105.  *      in the call back queue.
  106.  *
  107.  *----------------------------------------------------------------------
  108.  */
  109.  
  110. void
  111. Prof_Enable(procPtr, buffer, bufSize, offset, scale)
  112.     Proc_ControlBlock *procPtr;
  113.     short *buffer;
  114.     int bufSize;
  115.     int offset;
  116.     int scale;
  117. {
  118.  
  119.  
  120. #if 0
  121.     printf("Prof_Enable(p = %x, id = %x, buffer = %08x, bufSize = %d, offset = %d, scale = %d\n",
  122.     procPtr, procPtr->processID, buffer, bufSize, offset, scale);
  123.     printf("profCount = %d\n", profCount);
  124. #endif    
  125.  
  126.     assert(procPtr != (Proc_ControlBlock *) NIL);
  127.     LOCK_MONITOR;
  128.     if (scale != 0 && scale != 1 && procPtr->Prof_Scale == 0) {
  129.     /*
  130.      * If the profile routine has not already been scheduled for invocation
  131.      * by the call back timer, then schedule it.
  132.      */
  133.     if (profCount == 0) {
  134.         profTimer_QueueElement.interval = 20 * timer_IntOneMillisecond;
  135.         Timer_ScheduleRoutine(&profTimer_QueueElement, TRUE);
  136.     }
  137.     ++profCount;
  138.     } else if ((scale == 0 || scale == 1) && procPtr->Prof_Scale != 0) {
  139.     assert(profCount > 0);
  140.     /*
  141.      * Disable profiling.  If there are no other processes being profiled,
  142.      * then remove tick() from the call back queue.
  143.      */
  144.     --profCount;
  145.     if (profCount == 0) {
  146.         Timer_DescheduleRoutine(&profTimer_QueueElement);
  147.     }
  148.     }
  149.     procPtr->Prof_Buffer = buffer;
  150.     procPtr->Prof_BufferSize = bufSize;
  151.     procPtr->Prof_Offset = offset;
  152.     procPtr->Prof_Scale = scale;
  153.     procPtr->Prof_PC = 0;
  154.     UNLOCK_MONITOR;
  155.     return;
  156. }
  157.  
  158. /*
  159.  *----------------------------------------------------------------------
  160.  *
  161.  * tick --
  162.  *
  163.  *      If any processes are scheduled for profiling this routine
  164.  *      is called on each call back timer interrupt.
  165.  *
  166.  * Results:
  167.  *      Always returns SUCCESS.
  168.  * 
  169.  * Side effects:
  170.  *
  171.  *      If the current process is scheduled for profiling then the 
  172.  *      specialHandling flag is set, and the current PC is recorded in
  173.  *      the process control block.
  174.  *
  175.  *----------------------------------------------------------------------
  176.  */ 
  177.  
  178. /*ARGSUSED*/
  179. static void
  180. tick(time, clientData)
  181.     Timer_Ticks time;
  182.     ClientData clientData;
  183. {
  184.     Proc_ControlBlock *curProcPtr;
  185.  
  186.     LOCK_MONITOR;
  187.     assert(profCount != 0);
  188. #if 0    
  189.     if (profCount == 0) {
  190.     printf("Descheduling profil timer\n");
  191.     Timer_DescheduleRoutine(&profTimer_QueueElement);
  192.     return;
  193.     }
  194. #endif    
  195. #if 0
  196.     printf("Prof tick, mach_KernelMode = %d, profCount = %d\n",
  197.     mach_KernelMode, profCount);
  198. #endif
  199.     assert(clientData == profTimer_QueueElement.clientData);
  200.     if (!mach_KernelMode) {
  201.     assert(profCount);
  202.     curProcPtr = Proc_GetCurrentProc();
  203.     assert(curProcPtr !=  (Proc_ControlBlock *) NIL);
  204. #if 0
  205.     printf("Prof tick, scale=%d, pc=%08x, profCount = %d\n",
  206.         curProcPtr->Prof_Scale,    curProcPtr->Prof_PC, profCount);
  207. #endif
  208.     if (curProcPtr->Prof_Scale >= 2) {
  209.         curProcPtr->specialHandling = TRUE;
  210.     }
  211.     }
  212.     Timer_ScheduleRoutine(&profTimer_QueueElement, TRUE);
  213.     UNLOCK_MONITOR;
  214.     return;
  215. }
  216.  
  217. /*
  218.  *----------------------------------------------------------------------
  219.  *
  220.  * Prof_RecordPC --
  221.  *
  222.  *
  223.  *
  224.  * Results:
  225.  *      None.
  226.  * 
  227.  * Side effects:
  228.  *      None.
  229.  *  
  230.  *----------------------------------------------------------------------
  231.  */ 
  232.  
  233. void
  234. Prof_RecordPC(procPtr)
  235.     Proc_ControlBlock *procPtr;
  236. {
  237.     short *ptr;
  238.     union {
  239.     short shrt;
  240.     char c[2];
  241.     } u;
  242.  
  243.     assert(procPtr->Prof_Scale);
  244.     ptr = &procPtr->Prof_Buffer[(((procPtr->Prof_PC -
  245.         procPtr->Prof_Offset) * procPtr->Prof_Scale) >> 16) / sizeof(*ptr)];
  246.     /*
  247.      * Make sure the pointer is in the proper range.
  248.      */
  249.     if (ptr < procPtr->Prof_Buffer || ptr >=
  250.         &procPtr->Prof_Buffer[procPtr->Prof_BufferSize/sizeof(short)]) {
  251.     procPtr->Prof_PC = 0;
  252.     return;
  253.     }
  254.     /*
  255.      * Copy the counter in, increment it, and copy it back out.
  256.      */
  257.     if (Vm_CopyInProc(sizeof(short), procPtr, (Address) ptr, u.c, 1)
  258.       != SUCCESS) {
  259.     Prof_Disable(procPtr);
  260.     return;
  261.     }
  262.     ++u.shrt;
  263.  
  264. #if 0
  265.     printf("Prof_RecordPC(), shrt = %d,  profCount = %d\n", u.shrt, profCount);
  266. #endif    
  267.  
  268.     if (Vm_CopyOutProc(sizeof(short), u.c, 1, procPtr, (Address) ptr)
  269.       != SUCCESS) {
  270.     Prof_Disable(procPtr);
  271.     return;
  272.     }
  273.     procPtr->Prof_PC = 0;
  274.     return;
  275. }
  276.  
  277.  
  278. /*
  279.  *----------------------------------------------------------------------
  280.  *
  281.  * Prof_Disable --
  282.  *
  283.  *    Disable profiling of a process.
  284.  *
  285.  * Results:
  286.  *    None.
  287.  *
  288.  * Side effects:
  289.  *      Clears the Prof_Scale and Prof_PC fields of the PCB, and decrements
  290.  *      the count of profiled processes.  If no other processes are being
  291.  *      profiled, removes the profil timer from the callback timer queue.
  292.  *
  293.  *----------------------------------------------------------------------
  294.  */
  295.  
  296. void
  297. Prof_Disable(procPtr)
  298.     Proc_ControlBlock *procPtr;
  299. {
  300.     LOCK_MONITOR;
  301.     if (procPtr->Prof_Scale != 0) {
  302.     assert(profCount > 0);
  303.     procPtr->Prof_Scale = 0;
  304.     procPtr->Prof_PC = 0;
  305.     --profCount;
  306.     if (profCount == 0) {
  307.         Timer_DescheduleRoutine(&profTimer_QueueElement);
  308.     }
  309.     }
  310.     UNLOCK_MONITOR;
  311.     return;
  312. }
  313.  
  314.